package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Long getFanCountByUserId(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        Long count = this.mongoTemplate.count(query,UserLike.class);
        return count;
    }

    /**
     * 查询用户喜欢数据
     * @param userId
     * @param page
     * @param type
     *      1 互相关注
     *      2 我关注
     *      3 粉丝
     *      4 谁看过我
     * @return
     */
    @Override
    public List<UserLike> findUserLikes(Long userId, Integer type, Integer page, Integer pagesize) {
        List<UserLike> userLikes = new ArrayList<>();
        switch (type){
            //互相喜欢
            case 1:{
                //查询自己喜欢的用户ID列表
                Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
                userLikes = mongoTemplate.find(query, UserLike.class);
                List<Long> likeUserIds = userLikes.stream().map(u -> u.getLikeUserId()).collect(Collectors.toList());
                //查询自己喜欢且喜欢自己的用户列表
                query = Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId).and("isLike").is(true));
                query.with(PageRequest.of(page - 1,pagesize, Sort.by(Sort.Order.desc("created"))));
                userLikes = mongoTemplate.find(query,UserLike.class);
                break;
            }
            //喜欢
            case 2:{
                Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                                .with(PageRequest.of(page - 1,pagesize, Sort.by(Sort.Order.desc("created"))));
                userLikes = mongoTemplate.find(query,UserLike.class);
                break;
            }
            //粉丝
            case 3:{
                Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                        .with(PageRequest.of(page - 1,pagesize, Sort.by(Sort.Order.desc("created"))));
                userLikes = mongoTemplate.find(query,UserLike.class);
                break;
            }
            default:
                break;
        }
        return userLikes;
    }

    /**
     * 删除喜欢数据
     * @param userId
     * @param friendUserId
     * @return
     */
    @Override
    public Boolean deleteUserLike(Long userId, Long friendUserId) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(friendUserId));
            mongoTemplate.remove(query,UserLike.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
